import json
import re
import requests
head={
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36 Edg/99.0.1150.30",
        "cookie":"FZ_STROAGE.ketangpai.com=eyJTRUVTSU9OSUQiOiI2YTIxZGViMGQ1MThlZGE2IiwiU0VFU0lPTkRBVEUiOjE2NDYyNzkwODMxODh9; ARK_ID=undefined; ketangpai_home_slb=69fe0169927fb731c0cb96fa3ba1db0c; PHPSESSID=dg6l1psl62smhv8tir1kp57t16; ketangpai_home_remember=think%3A%7B%22username%22%3A%22MDAwMDAwMDAwMLOGy5iH379phd6Yl7LPgZ4%22%2C%22expire%22%3A%22MDAwMDAwMDAwMLOGvZSHz7dphbic3rHMdZ4%22%2C%22token%22%3A%22MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p-q6iZu2yrn4uNhJ3KedDYk7ivboS4jpSyz4mShLeZ3n6gYW0%22%2C%22sign%22%3A%22a1c07c6bf2acb5cf106694db24f648e5%22%7D"
        #"cookie":"SESSION=5459a040-3c90-42d5-9fe2-cfa4765cab3f; __pstsid__=b8f78877a874fe0eca6f273364469af5"

    }
def getname(i):
    url=f'https://v4.ketangpai.com/StudentsApi/lists?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&txt=&page={i}'
    #url="http://jwxt.jmu.edu.cn/student/for-std/course-table/semester/181/print-data/0/4759"

    response=requests.get(url=url,headers=head)
    str= json.dumps(response.json(), ensure_ascii=False)
    #print(str)
    #print(type(str))
    myname=re.compile(r'.*?"username":\s"(?P<name>.*?)",.*?',re.S)
    result=re.finditer(myname,str)
    for it in result:
        print(it.group("name"))
def getid(i):
    url=f'https://v4.ketangpai.com/StudentsApi/lists?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&txt=&page={i}'
    #url="http://jwxt.jmu.edu.cn/student/for-std/course-table/semester/181/print-data/0/4759"

    response=requests.get(url=url,headers=head)
    str= json.dumps(response.json(), ensure_ascii=False)
    #print(str)
    #print(type(str))
    myid=re.compile(r'.*?"id":\s"(?P<id>.*?)",.*?',re.S)
    result=re.finditer(myid,str)
    for it in result:
        print(it.group("id"))

def getkaoqing(url):
    #url="https://openapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2pZiGubtohaVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    #url="https://openapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2qdyG36dshNtyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    #url="https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2qd6Hz7-yhqVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    #url="https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid=MDAwMDAwMDAwMLR2qd6Hz7-yhqVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    guize=re.compile(r'username":\s"(?P<name>.*?)",.*?,\s"state":\s"(?P<state>\d)"')
    result = re.finditer(guize, str)
    #print(str)#打印html页面
    for it in result:
         name=it.group("name")
         state=it.group("state")
         #print(name,state)     #打印所有考勤名单
         if state=="1":#如果state==1则未考勤
            print(name)
            f.write(name+"\n")

def getkaoqingid():
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    print(str)
    idguize=re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"',re.S)
    result=re.finditer(idguize,str)
    for it in result:
        print(it.group("id"))
        print(it.group("title"))
    print(str)

def getshujukukaoqin():
    print("以下是数据库未考勤名单！！！")
    f.write("以下是数据库未考勤名单！！！\n")
    url = "https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZmG37NqhrVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    #print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        #print(it.group("id"))
        print(it.group("title"))#打印时间
        f.write(it.group("title") + "\n")
        url=f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
        getkaoqing(url)
    #print(str)#打印html页面

def getcaozuoxitongkaoqin():
    print("以下是操作系统未考勤名单")
    f.write("\n\n以下是操作系统未考勤名单\n")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiGz6tohctyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        # print(it.group("id"))
        print(it.group("title"))  # 打印时间
        f.write(it.group("title") + "\n")
        url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
        getkaoqing(url)
    # print(str)#打印html页面

def getmakesikaoqin():
    print("以下是马克思原理未考勤名单")
    f.write("\n\n以下是马克思原理未考勤名单\n")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiG381shMtyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        # print(it.group("id"))
        print(it.group("title"))  # 打印时间
        f.write(it.group("title") + "\n")
        url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
        getkaoqing(url)
    # print(str)#打印html页面

def getgailulun():
    print("以下是概率论未考勤名单")
    f.write("\n\n以下是概率论未考勤名单\n")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiGz7tthrVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        # print(it.group("id"))
        print(it.group("title"))  # 打印时间
        f.write(it.group("title") + "\n")
        url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
        getkaoqing(url)
    # print(str)#打印html页面

def getjisuanjiwangluo():
    print("以下是计算机网络未考勤名单")
    f.write("\n\n以下是计算机网络未考勤名单\n")
    url="https://jmuopenapi.ketangpai.com/SummaryApi/attence?courseid=MDAwMDAwMDAwMLOGtZiGz6tohKVyoQ&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeV3n6GdmLIip5w"
    response = requests.get(url=url, headers=head)
    str = json.dumps(response.json(), ensure_ascii=False)
    # print(str)
    idguize = re.compile(r'"id":\s"(?P<id>.*?)",\s"title":\s"(?P<title>.*?)"', re.S)
    result = re.finditer(idguize, str)
    for it in result:
        # print(it.group("id"))
        print(it.group("title"))  # 打印时间
        f.write(it.group("title") + "\n")
        url = f'https://jmuopenapi.ketangpai.com/AttenceApi/getAttenceDetailInfo?attenceid={it.group("id")}&token=MDAwMDAwMDAwMMurrpWavLehhs1-lrK5o9qD3YOVepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p6nHaZrnyrnZWiiJnKebqUmryXpIDOpNyyz4mThKeLl3-sgmDIip5w'
        getkaoqing(url)
    # print(str)#打印html页面

if __name__ == '__main__':
    # for i in range(1,8):
    #     getname(i)
    #     getid(i)
    #getkaoqing()
    #getkaoqingid()
    f=open("kaoqin.txt","a+")

    getshujukukaoqin()#数据库原理考勤
    getcaozuoxitongkaoqin()#操作系统考勤
    getmakesikaoqin()#马克思原理考勤
    getgailulun()#概率论与数理统计考勤
    getjisuanjiwangluo()#计算机网络考勤

    f.close()